/**
 * @author Linrui
 * @since 2010.04.25
 */
package com.qeado.user.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.user.model.User;
import com.qeado.user.service.UserService;

@Scope("session")
@Controller
public class UserAction extends ActionSupport {
	
	@Autowired
	private UserService userService;
	@Autowired
	private SortService sortService;

	private User userDTO; // use for user register and user login
	private Sort sortDTO;  
	private String validationCode; // 验证码
	private String confirmPwd; // 确认密码，用于用户注册
	private String[] userCategory; // 用户类型 0：企业用户  1:校园用户
	private String validationStatu ; // show the validated code statu of user login  //  0:false 1:true
	
	public String getValidationStatu() {
		return validationStatu;
	}

	public void setValidationStatu(String validationStatu) {
		this.validationStatu = validationStatu;
	}

	public String[] getUserCategory() {
		return userCategory;
	}

	public void setUserCategory(String[] userCategory) {
		this.userCategory = userCategory;
	}

	public String getConfirmPwd() {
		return confirmPwd;
	}

	public void setConfirmPwd(String confirmPwd) {
		this.confirmPwd = confirmPwd;
	}

	public Sort getSortDTO() {
		return sortDTO;
	}

	public void setSortDTO(Sort sortDTO) {
		this.sortDTO = sortDTO;
	}
	
	public String getValidationCode() {
		return validationCode;
	}

	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}

	public User getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(User userDTO) {
		this.userDTO = userDTO;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	/* user register */
	public String register() throws Exception {
		String result = userService.insertUser(this.userDTO);
		if("success".equals(result)){
			return "registerSuccess";
		} else {
			return "registerFail";
		}
	}

	/* user login */
	public String login() throws Exception{
		if( ActionContext.getContext().getSession().get("rand") != null && ActionContext.getContext().getSession().get("rand").equals(this.validationCode) ) {
				User user = userService.queryByNameByPassword(userDTO.getUserName(), userDTO.getPassword());
				if(user != null) {
					// if you login successfully , put your own self-info into the Session named login_user ;
					ActionContext.getContext().getSession().put("login_user", user);
					this.validationStatu = ""; 
					return "Login_Success";
				} else {
					this.validationStatu = " 帐号或密码错误";
					return "Login_Fail";
				}			
		} else {
			this.validationStatu = "验证码输入错误";
			return "Login_Fail";
		}

	}
	
	/* user logout */
	public String logout() throws Exception {
		if( ActionContext.getContext().getSession().get("login_user") != null) {
			ActionContext.getContext().getSession().remove("login_user");
			return "Logout_Success";
		} else {
			return "Logout_Fail";
		}	
	}
	
	/* update user */
	public String updateUser() throws Exception{
		if( userService.updateUser(userDTO) )
			return "success";
		else
			return "visit_fail";		
	}
	
}
